package com.cc.blog.admin.event.subscriber;

import com.cc.blog.admin.event.ReadArticleEvent;
import com.cc.blog.admin.mapper.ArticleMapper;
import com.cc.blog.admin.mapper.StatisticsArticlePVMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

/**
 * 阅读文章消费者
 * @Author cc
 * @Date 2024/7/19 16:18
 */
@Component
@Slf4j
@AllArgsConstructor
public class ReadArticleSubscriber implements ApplicationListener<ReadArticleEvent> {

    private final ArticleMapper articleMapper;
    private final StatisticsArticlePVMapper articlePVMapper;

    @Override
    @Async("threadPoolTaskExecutor")
    public void onApplicationEvent(ReadArticleEvent event) {
        // 在这里处理收到的事件，可以是任何逻辑操作
        Long articleId = event.getArticleId();
        // 获取当前线程名称
        String threadName = Thread.currentThread().getName();
        log.info("==> threadName: {}", threadName);
        log.info("==> 文章阅读事件消费成功，articleId: {}", articleId);
        // 执行文章阅读量 +1
        articleMapper.increaseReadNum(articleId);
        log.info("==> 文章阅读量 +1 操作成功，articleId: {}", articleId);
        // 当日文章 PV 访问量 +1
        LocalDate currDate = LocalDate.now();
        articlePVMapper.increasePVCount(currDate);
        log.info("==> 当日文章 PV 访问量 +1 操作成功，date: {}", currDate);
    }
}
